"""Anonycoin --- Bitcoin simulator with anonymity throught group signature

Copyright (c) 2016 Daniel Pimentel <d4n1@member.fsf.org>

This file is part Anonycoin.

Anonycoin is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.

Anonycoin is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Anonycoin.  If not, see <http://www.gnu.org/licenses/>.
"""

from csv import reader


def tracking(blockchain):
    if blockchain == "bitcoin":
        blockchain_file = "bitcoin.csv"
        blockchain_track = "bitcoin-track.csv"
    else:
        blockchain_file = "anonycoin.csv"
        blockchain_track = "anonycoin-track.csv"
    wallet = []
    track = 0
    transaction = 0
    f = reader(open(blockchain_file, "r"), delimiter="\t")
    for l in f:
        wallet += [l[4]]
        transaction += 1
    for w in range(len(wallet)):
        found = open(blockchain_file).read().count(str(wallet[w]))
        print("[" + str(blockchain) + "] Found " + str(found) +
              " transaction(s) of wallet " + str(wallet[w]))
        if found > 1:
            track += 1
            with open(blockchain_track, "a", encoding="utf-8") as f:
                f.write(str(found) + "\n")
        else:
            with open(blockchain_track, "a", encoding="utf-8") as f:
                f.write("0 \n")
    print("[" + str(blockchain) + "] Found " + str(track) +
          " transaction(s) of " + str(transaction) + " that can be traced.")


def deanonymity():
    """Bitcoin deanonymity technics.
    blockchain - blockchains generated by miners
    """
    tracking("bitcoin")
    tracking("anonycoin")
